package com.kzd.gtgame.main.mine

import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.UserInvite
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.umeng.socialize.UMShareAPI
import kotlinx.android.synthetic.main.activity_mine_invite.*

class MineInviteActivity : OwnerViewModeActivity<BaseViewModel>() {

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_invite

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMineInviteBar)
    }

    override fun initView() {

        tvMineInviteSubmit.setOnClickAntiShakeListener {
            val userInvite = viewModel.userInviteLiveData.value
                ?.data?.data ?: return@setOnClickAntiShakeListener
            viewModel.userShare(
                activity = this,
                url = userInvite.friend_url,
                title = userInvite.share_title,
                desc = userInvite.share_content
            )
        }
    }

    override fun initData() {
        viewModel.userInvite()
    }

    override fun initViewMode() {
        viewModel.userInviteLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }

    private fun fillData(userInvite: UserInvite) {
        tvMineInviteCountPeople.text = userInvite.num.toString()
        tvMineInviteCountIntegral.text = userInvite.points
    }
}